<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" v-if="!inSide">
      <el-form-item label="吸毒人员" prop="userId">
        <user-drug-select v-model="queryParams.userId" placeholder="请选择吸毒人员" :onchange="handleQuery"/>
      </el-form-item>
      <el-form-item label="本人电话" prop="brlxdh">
        <el-input
          v-model="queryParams.brlxdh"
          placeholder="请输入本人联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始日期" prop="qsrq">
        <el-date-picker clearable
          v-model="queryParams.qsrq"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择起始日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="jsrq">
        <el-date-picker clearable
          v-model="queryParams.jsrq"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="担保人" prop="dbr">
        <el-input
          v-model="queryParams.dbr"
          placeholder="请输入担保人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择审核状态" clearable>
          <el-option
            v-for="dict in dict.type.shzt"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="!inSide">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['event:leave:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['event:leave:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['event:leave:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['event:leave:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="leaveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" v-if="!inSide"/>
      <el-table-column label="id" align="center" prop="leaveId" v-if="false"/>
      <el-table-column label="申请时间" align="center" prop="createTime" width="180" v-if="inSide"/>
      <el-table-column label="吸毒人员" align="center" prop="userInfo" v-if="!inSide"/>
      <el-table-column label="外出地" align="center" prop="wcdName" show-tooltip-when-overflow/>
      <el-table-column label="本人联系电话" align="center" prop="brlxdh" v-if="!inSide"/>
      <el-table-column label="家人联系电话" align="center" prop="jrlxdh" v-if="!inSide"/>
      <el-table-column label="起始日期" align="center" prop="qsrq" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qsrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束日期" align="center" prop="jsrq" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.jsrq, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.shzt" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="审批人" align="center" prop="sprInfo" v-if="!inSide"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['event:leave:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['event:leave:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改请假对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="吸毒人员" prop="userId">
          <user-drug-select v-model="form.userId" placeholder="请选吸毒人员" :readonly="inSide"/>
        </el-form-item>
        <el-form-item label="外出地" prop="wcd">
          <area-cascader v-model="form.wcd"/>
        </el-form-item>
        <el-form-item label="本人联系电话" prop="brlxdh">
          <el-input v-model="form.brlxdh" placeholder="请输入本人联系电话" />
        </el-form-item>
        <el-form-item label="家人联系电话" prop="jrlxdh">
          <el-input v-model="form.jrlxdh" placeholder="请输入家人联系电话" />
        </el-form-item>
        <el-form-item label="起始日期" prop="qsrq">
          <el-date-picker clearable
            v-model="form.qsrq"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择起始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" prop="jsrq">
          <el-date-picker clearable
            v-model="form.jsrq"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="担保人" prop="dbr">
          <el-input v-model="form.dbr" placeholder="请输入担保人" />
        </el-form-item>
        <el-form-item label="担保人电话" prop="dbrdh">
          <el-input v-model="form.dbrdh" placeholder="请输入担保人电话" />
        </el-form-item>
        <el-form-item label="请假事由" prop="qjsy">
          <el-input v-model="form.qjsy" placeholder="请输入请假事由" />
        </el-form-item>
        <el-form-item label="图片" prop="img">
          <image-upload v-model="form.img"/>
        </el-form-item>
        <el-form-item label="审核状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择审核状态">
            <el-option
              v-for="dict in dict.type.shzt"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审批人" prop="spr">
          <user-member-select v-model="form.spr" placeholder="请选择审批人"/>
        </el-form-item>
        <el-form-item label="审批单位" prop="spdw">
          <el-input v-model="form.spdw" placeholder="请输入审批单位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLeave, getLeave, delLeave, addLeave, updateLeave } from "@/api/event/leave";
import sideView from "@/views/event/mixin/sideView";

export default {
  name: "Leave",
  dicts: ['shzt'],
  mixins: [sideView],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 请假表格数据
      leaveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        wcd: undefined,
        brlxdh: undefined,
        jrlxdh: undefined,
        qsrq: undefined,
        jsrq: undefined,
        dbr: undefined,
        dbrdh: undefined,
        qjsy: undefined,
        img: undefined,
        status: undefined,
        spr: undefined,
        spdw: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "吸毒人员id不能为空", trigger: "blur" }
        ],
        wcd: [
          { required: true, message: "外出地不能为空", trigger: "blur" }
        ],
        brlxdh: [
          { required: true, message: "本人联系电话不能为空", trigger: "blur" }
        ],
        jrlxdh: [
          { required: true, message: "家人联系电话不能为空", trigger: "blur" }
        ],
        qsrq: [
          { required: true, message: "起始日期不能为空", trigger: "blur" }
        ],
        jsrq: [
          { required: true, message: "结束日期不能为空", trigger: "blur" }
        ],
        dbr: [
          { required: true, message: "担保人不能为空", trigger: "blur" }
        ],
        dbrdh: [
          { required: true, message: "担保人电话不能为空", trigger: "blur" }
        ],
        qjsy: [
          { required: true, message: "请假事由不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "审核状态不能为空", trigger: "change" }
        ],
        spr: [
          { required: true, message: "审批人不能为空", trigger: "blur" }
        ],
        spdw: [
          { required: true, message: "审批单位不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询请假列表 */
    getList() {
      this.loading = true;
      this.handleWhenInSide((userId)=> {
        this.queryParams.userId = userId
      })
      listLeave(this.queryParams).then(response => {
        this.leaveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        leaveId: undefined,
        userId: undefined,
        wcd: undefined,
        brlxdh: undefined,
        jrlxdh: undefined,
        qsrq: undefined,
        jsrq: undefined,
        dbr: undefined,
        dbrdh: undefined,
        qjsy: undefined,
        img: undefined,
        remark: undefined,
        status: undefined,
        spr: undefined,
        spdw: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.leaveId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加请假";
      this.handleWhenInSide((userId)=> {
        this.form.userId = userId
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const leaveId = row.leaveId || this.ids
      getLeave(leaveId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改请假";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.leaveId != null) {
            updateLeave(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addLeave(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const leaveIds = row.leaveId || this.ids;
      this.$modal.confirm('是否确认删除请假编号为"' + leaveIds + '"的数据项？').then(() => {
        this.loading = true;
        return delLeave(leaveIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('event/leave/export', {
        ...this.queryParams
      }, `leave_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
